package com.yilin.platform.controller;

import com.google.gson.Gson;
import com.yilin.platform.base_core.application.BaseController;
import com.yilin.platform.base_core.application.ReturnData;
import com.yilin.platform.base_core.commons.exception.BusinessException;
import com.yilin.platform.base_core.commons.utils.ParamUtil;
import com.yilin.platform.base_core.interceptor.EncryptInterceptor;
import com.yilin.platform.base_core.interceptor.NoBindingFilter;
import com.yilin.platform.base_core.interceptor.SkipLoginVerify;
import com.yilin.platform.entity.weChat.WeChatTradeRecord;
import com.yilin.platform.service.IWeChatService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/platform/weChat/")
public class WeChatController extends BaseController {
    @Resource
    private IWeChatService weChatService;
    /**
     * 支付宝下单
     * */
    @PostMapping(value = "tradePaymentOrder")
    @EncryptInterceptor
    public ReturnData< Map<String, Object> > tradePaymentOrder(WeChatTradeRecord weChatTradeRecord, String trminalType) {
        ParamUtil.isNull(weChatTradeRecord.getTradeNo(), "本地流水号为空！");
        ParamUtil.isNull(weChatTradeRecord.getTradeName(), "交易名称为空！");
        ParamUtil.isNull(weChatTradeRecord.getOutOrganizeId(), "付款组织为空！");
        ParamUtil.isNull(weChatTradeRecord.getOutAcctName(), "付款人名称为空！");
        ParamUtil.isNull(weChatTradeRecord.getInOrganizeId(), "收款人组织为空！");
        ParamUtil.isNull(weChatTradeRecord.getInAcctName(), "收款人名称为空！");
        ParamUtil.isNull(weChatTradeRecord.getTranAmount(), "交易金额为空！");
        ParamUtil.isNull(weChatTradeRecord.getUserType(), "用户类型为空！");
        WeChatTradeRecord weChatTradeRecordOld=weChatService.getWeChatTradeRecordByTradeNo(weChatTradeRecord.getTradeNo());
        if(weChatTradeRecordOld!=null){
           if(weChatTradeRecord.getWeChatTradeStatus()==WeChatTradeRecord.WeChatTradeStatus.handle){
                throw new BusinessException("平台：该订单号正在处理中请稍后重试：");
            }else if(weChatTradeRecord.getWeChatTradeStatus()==WeChatTradeRecord.WeChatTradeStatus.success){
                throw new BusinessException("平台：该订单单号以存在");
            }
            weChatTradeRecord.setId(weChatTradeRecordOld.getId());
        }
        Map<String, Object> object=null;
        if(trminalType.equals("app")){
            object=weChatService.wxChartAppTransactions(weChatTradeRecord);
        }else if(trminalType.equals("web")){
            object=weChatService.wxChartAppTransactionsH5(weChatTradeRecord);
        }else{
            return jsonView(false, "识别设备类型错误");
        }
        return jsonView(SUCCESS_CODE, "下单成功", object);
    }
    /**
     * 微信下单查询
     * */
    @PostMapping(value = "wxChartAppQueryTransactions")
    @EncryptInterceptor
    public ReturnData<WeChatTradeRecord> wxChartAppQueryTransactions(String tradeNo) {
        ParamUtil.isNull(tradeNo, "流水号为空！");
        WeChatTradeRecord body= weChatService.wxChartAppQueryTransactions(tradeNo);
        return jsonView(SUCCESS_CODE, "查询成功", body);
    }
    /**
     * 微信退款
     * */
    @PostMapping(value = "wxChartAppRefund")
    @EncryptInterceptor
    public ReturnData<WeChatTradeRecord> wxChartAppRefund(WeChatTradeRecord weChatTradeRecord) {
        ParamUtil.isNull(weChatTradeRecord.getTradeNo(), "本地流水号为空！");
        ParamUtil.isNull(weChatTradeRecord.getTradeName(), "交易名称为空！");
        ParamUtil.isNull(weChatTradeRecord.getOutOrganizeId(), "付款组织为空！");
        ParamUtil.isNull(weChatTradeRecord.getOutAcctName(), "付款人名称为空！");
        ParamUtil.isNull(weChatTradeRecord.getInOrganizeId(), "收款人组织为空！");
        ParamUtil.isNull(weChatTradeRecord.getInAcctName(), "收款人名称为空！");
        ParamUtil.isNull(weChatTradeRecord.getRefundOriginalTradeNo(), "原交易订单号为空！");
        WeChatTradeRecord weChatTradeRecordOrging=weChatService.getWeChatTradeRecordByTradeNo(weChatTradeRecord.getRefundOriginalTradeNo());
        ParamUtil.isNull(weChatTradeRecordOrging, "原交易订单为空！");
        if(weChatTradeRecordOrging.getWeChatTradeStatus()== WeChatTradeRecord.WeChatTradeStatus.handle){
            weChatTradeRecordOrging=weChatService.wxChartAppQueryTransactions(weChatTradeRecordOrging.getTradeNo());
        }
        ParamUtil.notEquals("交易单未支付成功，退款失败",weChatTradeRecordOrging.getWeChatTradeStatus(),WeChatTradeRecord.WeChatTradeStatus.success);
        ParamUtil.notEquals("原付款人和收款人不是同一个，退款失败",weChatTradeRecord.getInOrganizeId(),weChatTradeRecordOrging.getOutOrganizeId());
        WeChatTradeRecord body= weChatService.wxChartAppRefund(weChatTradeRecord,weChatTradeRecordOrging);
        return jsonView(SUCCESS_CODE, "退款成功", body);
    }

    /**
     * 微信退款查询
     * */
    @PostMapping(value = "wxChartAppQueryRefund")
    @EncryptInterceptor
    public ReturnData<WeChatTradeRecord> wxChartAppQueryRefund(String tradeNo) {
        ParamUtil.isNull(tradeNo, "流水号为空！");
        WeChatTradeRecord body= weChatService.wxChartAppQueryRefund(tradeNo);
        return jsonView(SUCCESS_CODE, "查询成功", body);
    }

    /**
     * 微信支付回调
     */
    @PostMapping(value = "weChartPayBack/{userType}")
    @SkipLoginVerify
    @NoBindingFilter  //这个注解是用来不走过滤器的，不然body会验签失败
    public String weChartPayBack(@PathVariable("userType") String userType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = weChatService.weChatCallback(request, response, userType,"payment");
        return new Gson().toJson(map);

    }


    /**
     * 微信支付退款回调
     */
    @PostMapping(value = "weChartRefundBack/{userType}")
    @SkipLoginVerify
    @NoBindingFilter
    public String weChartRefundBack(@PathVariable("userType") String userType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object>  map = weChatService.weChatCallback(request, response, userType,"refund");
        return new Gson().toJson(map);
    }

}
